<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            width: 100%;
            height: 100%;
            background: url(img.jpg) no-repeat 0 0;
            background-size: cover;
        }
        
        #register {
            padding: 20px 40px;
            width: 260px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(255, 255, 255, .8);
            line-height: 20px;
            border-radius: 20px;
        }
        
        #register #userName {
            margin-bottom: 5px;
            margin-top: 7px;
        }
        
        #register .password {
            margin-bottom: 5px;
        }
        
        #register #code {
            margin-left: 16px;
            outline: none;
            width: 162px;
        }
        
        #register #btn {
            float: right;
            margin: 10px;
        }
        
        button {
            width: 50px;
        }
        
        input {
            outline: none;
        }
        
        h3 {
            margin-bottom: 5px;
        }
        
        #rentHolidy {
            font-size: 13px;
            margin-top: 5px;
        }
        
        #rentHolidy #data {
            margin-top: 3px;
        }
        
        #username {
            margin-left: -10px;
        }
        
        #password {
            margin-left: 5px;
        }
    </style>
</head>

<body>
    <div id="register">
        <h3>登录</h3>
        <hr>
        <div id="userName">
            <span>用户名：</span>
            <input type="text" placeholder="请输入用户名" id="username">
        </div>
        <div class="password">
            <span>密码：</span>
            <input type="password" placeholder="请输入密码" id="password">
        </div>
        <!-- <div id="userSex">
            <span>性别:</span>
            <input type="radio" name="sex" id="data-man" checked>男
            <input type="radio" name="sex" id="data-female">女
        </div> -->
        <div id="rentHolidy">
            <input type="radio" id="data">30天内免登录
        </div>
        <div id="btn">
            <button id="enter">登录</button>
            <button id="enroll"> 注册</button>
        </div>
    </div>
    <script src="lib/jquery.min.js"></script>
    <script>
        let address = "http://10.35.170.159:82"
            // let address = "http://192.168.1.102:82";
            //注册
        $("#enroll").click(function() {
                location.href = `${address}/login.html`
            })
            //登录
        $("#enter").click(function() {
            if ($("#username").val() && $("#password").val()) {
                $.ajax({
                    url: `${address}/login`,
                    data: {
                        username: $("#username").val(),
                        password: $("#password").val(),
                    },
                    success(data) {
                        if (data === "2") {
                            alert("用户名或密码错误")
                        } else {
                            //记住30天内免登录状态逻辑
                            if ($("#data")[0].checked) {
                                let date = new Date
                                date.setDate(date.getDate() + 30)
                                document.cookie = `userInfo=${data};expires=${date}`
                            }
                            //本地存储
                            sessionStorage.setItem("userInfo", data);
                            location.href = `${address}/student.html`
                        }
                    }
                })
            }

        })
    </script>
</body>

</html>